package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.Announcement;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.*;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private AnnouncementApi announcementApi;

    @DubboReference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 根据环信id查询用户详情
     */
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //1、根据环信id查询用户
        User user = userApi.findByHuanxin(huanxinId);
        //2、根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo); //copy同名同类型的属性
        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    /**
     * 添加好友关系
     * @param friendId
     */
    public void contacts(Long friendId) {
        //判断数据库中是否存在好友关系(不能单方面存在)
//        Boolean isNew = friendApi.exists(UserHolder.getUserId(), friendId);
        //1.将好友注册到环信
//        if (!isNew) {
            Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                    Constants.HX_USER_PREFIX + friendId);
            if (!aBoolean) {
                throw new BusinessException(ErrorResult.error());
            }
            //2.如果注册成功
            friendApi.save(UserHolder.getUserId(), friendId);
    }
    /**
     * 分页查询联系人
     */
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1.调用API查询当前用户的好友数据
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(),page,pagesize);
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //2.提取数据列表中的好友id
        List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //3.调用UserInfoApi查询好友的用户详情
        UserInfo info = new UserInfo();
        info.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds,info);
        //4.构造vo对象
        List<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo = map.get(friend.getFriendId());
            if (userInfo != null){
                ContactVo vo = ContactVo.init(userInfo);
                vos.add(vo);
            }
        }
        PageResult pageResult = new PageResult(page, pagesize, 0L, vos);
        System.out.println(pageResult);
        return pageResult;
    }
    /**
     * 公告分页
     */
    public PageResult announcements(Integer page, Integer pagesize) {
        //查询所有
        Page<Announcement> iPage = announcementApi.findAnnouncement(page,pagesize);
        List<Announcement> list = iPage.getRecords();
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
//        List<AnnouncementVo> vos = new ArrayList<>();
//        for (Announcement announcement : list) {
//            if (announcement != null) {
//                AnnouncementVo vo = AnnouncementVo.init(announcement);
//                vos.add(vo);
//            }
//        }

        PageResult pageResult = new PageResult(page, pagesize,3l,iPage.getRecords());
        return pageResult;
    }
    /**
     * 喜欢列表
     */
    public PageResult lovesPage(Integer page, Integer pagesize) {
        //根据用户id查询喜欢列表
        List<Comment> list = commentApi.findUserIdComment(UserHolder.getUserId(), CommentType.LOVE,page,pagesize);
        if (list == null){
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(list, "publishUserId", Long.class);
        //根据id查找人的
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            if (userInfo != null){
                CommentVo vo = CommentVo.init(userInfo,comment);
                vos.add(vo);
            }
        }
        System.out.println(vos);
//        Integer count = vos.size();
        PageResult pageResult = new PageResult(page, pagesize,0L, vos);
        System.out.println(pageResult);
        return pageResult;
    }

    /**
     * 点赞列表
     */
    public PageResult likesPage(Integer page, Integer pagesize) {
        //根据用户id查询喜欢列表
        List<Comment> list = commentApi.findUserIdComment(UserHolder.getUserId(), CommentType.LIKE,page,pagesize);
        if (list == null){
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(list, "publishUserId", Long.class);

        //根据id查找人的
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            if (userInfo != null){
                CommentVo vo = CommentVo.init(userInfo,comment);
                vos.add(vo);
            }
        }
        System.out.println(vos);
        Long count = Long.valueOf(vos.size());
        PageResult pageResult = new PageResult(page, pagesize, 0L, vos);
        System.out.println(pageResult);
        return pageResult;
    }
    /**
     * 评论列表
     */
    public PageResult commentsPage(Integer page, Integer pagesize) {
        //根据用户id查询喜欢列表
        List<Comment> list = commentApi.findUserIdComment(UserHolder.getUserId(), CommentType.COMMENT, page, pagesize);
        if (list == null) {
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(list, "publishUserId", Long.class);

        //根据id查找人的
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        System.out.println(vos);
        Long count = Long.valueOf(vos.size());
        PageResult pageResult = new PageResult(page, pagesize, 0L, vos);
        System.out.println(pageResult);
        return pageResult;
    }
}

